package logic

import (
	"2210a/commod/commodrpc/commodrpc"
	"2210a/commod/commodrpc/internal/svc"
	"2210a/commod/models/mysql"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopAddLogic) ShopAdd(in *commodrpc.ShopAddRequest) (*commodrpc.ShopAddResponse, error) {
	// todo: add your logic here and delete this line
	//var users mysql.Users
	//mysql.Db.Debug().Where("username = ?", in.Username).Find(&users)
	//if users.ID != 0 {
	//	return nil, fmt.Errorf("账号不存在")
	//}
	var FindProduct mysql.Products
	mysql.Db.Debug().Where("user_id = ?", in.UserId).Find(&FindProduct)
	if FindProduct.ID == 0 {
		return nil, fmt.Errorf("用户不存在")
	}
	products := mysql.Products{
		UserId:         in.UserId,
		Avatar:         in.Avatar,
		ShopName:       in.ShopName,
		Price:          in.Price,
		Gender:         in.Gender,
		ShopNavigation: in.ShopNavigation,
		Address:        in.Address,
		Quantity:       in.Quantity,
		Introduce:      in.Introduce,
		Status:         in.Status,
	}
	mysql.Db.Debug().Create(&products)
	return &commodrpc.ShopAddResponse{Id: int64(products.ID)}, nil
}
